
package com.joysuch.wwyt.audit.support;

import java.io.Serializable;

import javax.annotation.Resource;
import javax.persistence.EntityManager;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.query.EscapeCharacter;
import org.springframework.data.jpa.repository.support.JpaRepositoryFactory;
import org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean;
import org.springframework.data.querydsl.SimpleEntityPathResolver;
import org.springframework.data.repository.core.support.RepositoryFactorySupport;

//@Component
public class SubRepositoryFactoryBean<R extends JpaRepository<T, I>, T, I extends Serializable>
        extends JpaRepositoryFactoryBean<R, T, I> {

    @Resource
    private RepositoryPostProcessor repositoryPostProcessor;

    public SubRepositoryFactoryBean(Class<? extends R> repositoryInterface) {
        super(repositoryInterface);
    }

    protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) {
        JpaRepositoryFactory jpaFac = new JpaRepositoryFactory(entityManager);
        jpaFac.addRepositoryProxyPostProcessor(repositoryPostProcessor);
        // jpaFac.addRepositoryProxyPostProcessor(new RepositoryPostProcessor());
        jpaFac.setEscapeCharacter(EscapeCharacter.DEFAULT);
        jpaFac.setEntityPathResolver(new SimpleEntityPathResolver(".dsl"));
        return jpaFac;
    }

    public RepositoryPostProcessor getRepositoryPostProcessor() {
        return repositoryPostProcessor;
    }

    public void setRepositoryPostProcessor(RepositoryPostProcessor repositoryPostProcessor) {
        this.repositoryPostProcessor = repositoryPostProcessor;
    }

}
